package com.ruoyi.project.sale.order.controller;
/*1*/
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sale.customer.domain.Customer;
import com.ruoyi.project.sale.order.domain.Order;
import com.ruoyi.project.sale.order.service.OrderService;
import com.ruoyi.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sale/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    private String prefix = "sale/order";

    @RequiresPermissions("sale:order:view")
    @GetMapping()
    public String showCustomer()
    {
        return prefix + "/view";
    }

    @RequiresPermissions("sale:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.findAll(order);
        return getDataTable(list);
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("sale:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Order order){
        Integer index = orderService.saveOrder(order);
        try {
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("添加失败");
    }


    /**
     * 修改订单 数据回显
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("order", orderService.findOne(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("sale:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Order order)
    {
        Integer index = orderService.changeOrder(order);
        try {
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage()); //将错误信息反馈给前台，e.printStackTrace();就要注销
        }
        return error("修改失败");
    }

    //删除的操作
    @RequiresPermissions("sale:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){

        Integer[] arr = Convert.toIntArray(ids);

        try {
            int index=0;
            if (arr.length==1){ //删除的单条数据
                index=orderService.removeOrder(arr[0]);
            }else {
                index=orderService.removeOrders(arr);
            }
            if (index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("删除失败");
    }

    //导出功能
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("sale:order:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order)
    {
        List<Order> list = orderService.findAll(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "角色数据");
    }

}
